package lib

import (
	"fmt"
	"os"
	"time"

	rotatelogs "github.com/lestrrat/go-file-rotatelogs"
	"github.com/sirupsen/logrus"
)

// Logger .
var Logger *logrus.Logger

// NewLogger is Constructed
func init() {
	Logger = logrus.New()
	Logger.Formatter = new(logrus.JSONFormatter)
	Logger.SetReportCaller(true)
	mode := Config.Get("mode")

	if mode == "test" || mode == "release" {
		logPath := Config.GetString("logPath")
		logf, err := rotatelogs.New(
			logPath+"access.log.%Y%m%d",
			rotatelogs.WithMaxAge(24*time.Hour),
			rotatelogs.WithRotationTime(24*time.Hour),
		)
		if err != nil {
			fmt.Errorf("failed to create rotatelogs: %s", err)
			return
		}
		Logger.Out = logf
	} else {
		Logger.Out = os.Stdout
	}
}
